package com.funsport.controller.web;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.funsport.common.Result;
import com.funsport.dto.request.VenueBookingRequest;
import com.funsport.dto.response.AvailableTimeSlot;
import com.funsport.dto.response.VenueBookingResponse;
import com.funsport.service.IVenueBookingService;
import com.funsport.security.SecurityUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.List;

/**
 * Web端场地预订管理Controller
 */
@Slf4j
@RestController
@RequestMapping("/api/web/venue/booking")
@RequiredArgsConstructor
@Tag(name = "Web端场地预订管理")
public class WebVenueBookingController {
    
    private final IVenueBookingService venueBookingService;
    
    @Operation(summary = "获取所有预订列表（管理员）")
    @GetMapping("/list")
    @PreAuthorize("hasAnyRole('SUPER_ADMIN', 'ADMIN', 'VENUE_MANAGER')")
    public Result<Page<VenueBookingResponse>> getAllBookings(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String status,
            @RequestParam(required = false) Long venueId,
            @RequestParam(required = false) Long userId
    ) {
        Page<VenueBookingResponse> bookings = venueBookingService.getAllBookings(page, pageSize, status, venueId, userId);
        return Result.success(bookings);
    }
    
    @Operation(summary = "获取预订详情")
    @GetMapping("/{id}")
    @PreAuthorize("hasAnyRole('SUPER_ADMIN', 'ADMIN', 'VENUE_MANAGER')")
    public Result<VenueBookingResponse> getBookingDetail(@PathVariable Long id) {
        VenueBookingResponse booking = venueBookingService.getBookingDetail(id);
        return Result.success(booking);
    }
    
    @Operation(summary = "获取场地预订列表")
    @GetMapping("/venue/{venueId}")
    @PreAuthorize("hasAnyRole('SUPER_ADMIN', 'ADMIN', 'VENUE_MANAGER')")
    public Result<Page<VenueBookingResponse>> getVenueBookings(
            @PathVariable Long venueId,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String status
    ) {
        Page<VenueBookingResponse> bookings = venueBookingService.getVenueBookings(venueId, page, pageSize, status);
        return Result.success(bookings);
    }
    
    @Operation(summary = "获取场地在指定日期的预订")
    @GetMapping("/venue/{venueId}/date")
    @PreAuthorize("hasAnyRole('SUPER_ADMIN', 'ADMIN', 'VENUE_MANAGER')")
    public Result<List<VenueBookingResponse>> getBookingsByDate(
            @PathVariable Long venueId,
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime date
    ) {
        List<VenueBookingResponse> bookings = venueBookingService.getBookingsByDate(venueId, date);
        return Result.success(bookings);
    }
    
    @Operation(summary = "获取场地可用时间段")
    @GetMapping("/venue/{venueId}/available")
    @PreAuthorize("hasAnyRole('SUPER_ADMIN', 'ADMIN', 'VENUE_MANAGER')")
    public Result<List<AvailableTimeSlot>> getAvailableTimeSlots(
            @PathVariable Long venueId,
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime date
    ) {
        List<AvailableTimeSlot> slots = venueBookingService.getAvailableTimeSlots(venueId, date);
        return Result.success(slots);
    }
    
    @Operation(summary = "确认预订")
    @PostMapping("/{id}/confirm")
    @PreAuthorize("hasAnyRole('SUPER_ADMIN', 'ADMIN', 'VENUE_MANAGER')")
    public Result<Void> confirmBooking(@PathVariable Long id) {
        Long operatorId = SecurityUtils.getCurrentUserId();
        venueBookingService.confirmBooking(id, operatorId);
        return Result.success("预订已确认", null);
    }
    
    @Operation(summary = "取消预订")
    @PostMapping("/{id}/cancel")
    @PreAuthorize("hasAnyRole('SUPER_ADMIN', 'ADMIN', 'VENUE_MANAGER')")
    public Result<Void> cancelBooking(@PathVariable Long id) {
        Long operatorId = SecurityUtils.getCurrentUserId();
        venueBookingService.cancelBooking(id, operatorId);
        return Result.success("预订已取消", null);
    }
    
    @Operation(summary = "完成预订")
    @PostMapping("/{id}/complete")
    @PreAuthorize("hasAnyRole('SUPER_ADMIN', 'ADMIN', 'VENUE_MANAGER')")
    public Result<Void> completeBooking(@PathVariable Long id) {
        venueBookingService.completeBooking(id);
        return Result.success("预订已完成", null);
    }
}

